package com.yjy.mybase.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.yjy.mybase.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * Created by admin on 2016/11/1.
 */
public abstract class BaseFragment extends Fragment {
    protected View mRootView;
    protected Activity mActivity;
    private Unbinder mUnbinder;

    /**
     * 是否初始化过布局
     */
    protected boolean isViewInitiated;
    /**
     * 当前界面是否可见
     */
    protected boolean isVisibleToUser;
    /**
     * 是否加载过数据
     */
    protected boolean isDataInitiated;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareFetchData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (isVisibleToUser) {
            prepareFetchData();
        }
    }

    /**
     * 懒加载
     */
    public void lazyLoadData() {

    }

    /**
     * 判断懒加载条件
     */
    public void prepareFetchData() {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated)) {
            lazyLoadData();
            isDataInitiated = true;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(layout(), container, false);
        //ButterKnife注册
        mUnbinder = ButterKnife.bind(this, mRootView);
        mActivity = getActivity();
        onViewCreated();
        return mRootView;
    }

    /**
     * 初始化
     */
    protected abstract void onViewCreated();

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden)
            onPause();
        else
            onResume();
    }

    public View getRootView() {
        return mRootView;
    }

    protected abstract int layout();

    @Override
    public void onDestroy() {
        mActivity = null;
        //解除ButterKnife注册
        mUnbinder.unbind();
        super.onDestroy();
    }

    /**
     * 隐藏输入法
     *
     * @param view
     */
    public void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
        if (imm != null)
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 显示输入法
     *
     * @param view EditText
     */
    public void showKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
        if (imm != null)
            imm.showSoftInput(view, 0);
    }


    public void startService(Class clz) {
        Intent intent = new Intent(getActivity(), clz);
        getActivity().startService(intent);
    }

    public void startService(Class clz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clz);
        if (bundle != null)
            intent.putExtras(bundle);
        getActivity().startService(intent);
    }

    public void startActivity(Class clz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clz);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivity(intent);
    }

    public void startActivity(Class clz) {
        Intent intent = new Intent(getActivity(), clz);
        startActivity(intent);
    }

    public void startActivityForResult(Class clz, int requestCode) {
        Intent intent = new Intent(getActivity(), clz);
        startActivityForResult(intent, requestCode);
    }

    public void startActivityForResult(Class clz, Bundle bundle, int requestCode) {
        Intent intent = new Intent(getActivity(), clz);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    /**
     * Toast
     *
     * @param message 消息
     */
    public void toast(String message) {
        ToastUtils.show(message);
    }
}
